package nus.mtech.bill;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import nus.mtech.util.DateUtil;
import nus.mtech.util.StringUtil;

public class AccountManager {
	public static AccountManager accountManager;
	private HashMap<String, Account> accountMap;
	private static long accountUID = 1;
	private final String ACCT_PREFIX = "SA-";
	
	private AccountManager(){
		accountMap = new HashMap<String, Account>();
	}
	public static AccountManager getInstance(){
		if(accountManager == null)
			accountManager = new AccountManager();
		return accountManager;
	}
	
	public void addAccount(Account account){
		accountMap.put(account.getAccountNo(), account);
	}
	
	public Account getAccount(String accountNo){
		return accountMap.get(accountNo);
	}
	
	public Collection<Account> getAllAccounts(){
		return accountMap.values();
	}
	
	public Payment makePayment(String accountNo, double value) throws Exception{
		Account account = accountMap.get(accountNo);
		if(account == null){
			throw new Exception("Account ["+accountNo + "]  not found ");
		}
		Payment payment = new Payment(accountNo, value);
		account.addPayment(payment);
		return payment;
	}
	
	public Payment getPayment(String accountNo, String paymentId) throws Exception{
		Account account = accountMap.get(accountNo);
		if(account == null){
			throw new Exception("Account ["+accountNo + "]  not found ");
		}
		for(Payment payment:account.getPaymentList()){
			if(paymentId.equals(payment.getPaymentID()))
				return payment;
		}
		return null;
	}
	
	public List<Payment> getPayments(String accountNo) throws Exception{
		Account account = accountMap.get(accountNo);
		if(account == null){
			throw new Exception("Account ["+accountNo + "]  not found ");
		}
		return account.getPaymentList();
	}
	
	public Account createAccount(){
		Account account = new Account(createAccountNo());
		account.setCloseDate(new Date());
		account.setActiveStatus(true);
		addAccount(account);
		return account;
	}
	public Account CreateAccount(String customerId){
		Account account = createAccount();
		account.setCustomerId(customerId);
		return account;
	}
	
	public String getAccountUID (){
//		DecimalFormat df = new DecimalFormat("0000");
//		return df.format(accountUID++);
		return String.valueOf(accountUID++);
	}
	public void setAccountUID(int accountUID) {
		AccountManager.accountUID = accountUID;
	}
	public HashMap<String, Account> getAccountMap() {
		return accountMap;
	}
	public void setAccountMap(HashMap<String, Account> accountMap) {
		this.accountMap = accountMap;
	}
	
	//Alvin's accountNo creation
	//Added by Joe [5-Apr-2011]
	private String createAccountNo() {
		String tempAccountNo = StringUtil.leftPad("" + getAccountUID(), 4, "0");
		tempAccountNo = "-" + tempAccountNo.substring(0, 2) + "-"
				+ tempAccountNo.substring(2);
		tempAccountNo = ACCT_PREFIX + DateUtil.formatDate(new Date(), "yyyy")
				+ tempAccountNo;

		return tempAccountNo;
	}
}
